// index.js
// 获取应用实例
const app = getApp(),
system_info = wx.getSystemInfoSync()
Page({
  data: {
    dots: [], //小时
    scales: [], //刻度
    weekday: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"], //星期
    year: 0,
    month: 0,
    day: 0,
    hours: 0,
    minutes: 0,
    seconds: 0,
    date: 0,
    hour_line: 0,
    minute_line: 0,
    second_line: 0,
    box_size:400,//默认400px
    box_border:10,//border边框
    dot_size:40,//默认40px
  },
  // 事件处理函数
  bindViewTap() {},
  onLoad() {
    this.setTime();
    const size = system_info.windowWidth-this.data.box_border*2;
    this.setData({
      box_size:size,
      dots: this.getDots(),
      scales: this.getScales(),
    })
  },
  onReady() {
    setInterval(this.setTime,1000);
  },
  computeLeft: function (index) {
    const box_radius = (system_info.windowWidth-this.data.box_border*2)/2;//box半径
    const dot_radius = this.data.dot_size/2;//dot半径
    const radius = box_radius-dot_radius;
    const dot_radian = 360/12;//每个dot 对应的弧度数
    return parseFloat(radius + Math.sin((dot_radian * Math.PI / 180) * (index)) * (radius-dot_radius));
  },
  computeTop: function (index) {
    const box_radius = (system_info.windowWidth-this.data.box_border*2)/2;//box半径
    const dot_radius = this.data.dot_size/2;//dot半径
    const radius = box_radius-dot_radius;
    const dot_radian = 360/12;//每个dot 对应的弧度数
    return parseFloat(radius + Math.cos((dot_radian * Math.PI / 180) * (index)) * (radius-dot_radius));
  },
  getScales: function () {
    let scales = [];
    for (var i = 0; i < 60; i++) {
      const scale = parseFloat(i * 6 - 90);
      scales.push(scale);
    }
    return scales;
  },
  getDots: function () {
    return [{
        num: 6,
        left: this.computeLeft(0),
        top: this.computeTop(0)
      },
      {
        num: 5,
        left: this.computeLeft(1),
        top: this.computeTop(1)
      },
      {
        num: 4,
        left: this.computeLeft(2),
        top: this.computeTop(2)
      },
      {
        num: 3,
        left: this.computeLeft(3),
        top: this.computeTop(3)
      },
      {
        num: 2,
        left: this.computeLeft(4),
        top: this.computeTop(4)
      },
      {
        num: 1,
        left: this.computeLeft(5),
        top: this.computeTop(5)
      },
      {
        num: 12,
        left: this.computeLeft(6),
        top: this.computeTop(6)
      },
      {
        num: 11,
        left: this.computeLeft(7),
        top: this.computeTop(7)
      },
      {
        num: 10,
        left: this.computeLeft(8),
        top: this.computeTop(8)
      },
      {
        num: 9,
        left: this.computeLeft(9),
        top: this.computeTop(9)
      },
      {
        num: 8,
        left: this.computeLeft(10),
        top: this.computeTop(10)
      },
      {
        num: 7,
        left: this.computeLeft(11),
        top: this.computeTop(11)
      }
    ];
  },
  setTime: function () {
    const newDate = new Date();
    const hour_rotate = (newDate.getHours()*30-90) + (Math.floor(newDate.getMinutes() / 12) * 6);
    // console.log(newDate.getDate());
    this.setData({
      year: newDate.getFullYear(),
      month: newDate.getMonth() + 1,
      day: newDate.getDay(),
      hours: newDate.getHours(),
      minutes: newDate.getMinutes(),
      seconds: newDate.getSeconds(),
      date: newDate.getDate(),
      hour_line: hour_rotate,
      minute_line: (newDate.getMinutes()*6 - 90),
      second_line: (newDate.getSeconds()*6 - 90),
    });
  }
})